package com.dongdongmovie.service.impl;

import com.dongdongmovie.mapper.AdminUserMapper;
import com.dongdongmovie.mapper.UserRoleMapper;
import com.dongdongmovie.pojo.AdminUser;
import com.dongdongmovie.pojo.AdminUserExample;
import com.dongdongmovie.pojo.UserRoleExample;
import com.dongdongmovie.service.AdminUser_Service;
import com.dongdongmovie.util.MD5Utils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DubboService
public class AdminUser_ServiceImpl implements AdminUser_Service {

    //注入mapper
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    //查寻数据
    @Override
    public List<AdminUser> show() {
        return adminUserMapper.selectByExample(null);
    }

    //添加数据
    @Override
    public int add(AdminUser a, Integer[] roleIds) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String pwd = MD5Utils.getEncryptedPwd(a.getUserPwd());
        a.setUserPwd(pwd);
        adminUserMapper.add(a);
        userRoleMapper.add(a.getUserId(), roleIds);
        return 1;
    }

    //根据id查询数据
    @Override
    public AdminUser showbyId(Integer userId) {
        return adminUserMapper.selectByPrimaryKey(userId);
    }

    //查询用户所处角色
    @Override
    public List<Integer> showroleids(Integer userId) {
        return userRoleMapper.showroleids(userId);
    }

    //修改数据
    @Override
    public int update(AdminUser a, Integer[] roleIds) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String pwd = MD5Utils.getEncryptedPwd(a.getUserPwd());
        a.setUserPwd(pwd);
        adminUserMapper.updateByPrimaryKeySelective(a);
        UserRoleExample ue = new UserRoleExample();
        UserRoleExample.Criteria criteria = ue.createCriteria();
        criteria.andUserIdEqualTo(a.getUserId());
        userRoleMapper.deleteByExample(ue);
        userRoleMapper.add(a.getUserId(), roleIds);
        return 1;
    }

    //删除数据
    @Override
    public int delete(Integer userId) {
        adminUserMapper.deleteByPrimaryKey(userId);
        UserRoleExample ue = new UserRoleExample();
        UserRoleExample.Criteria criteria = ue.createCriteria();
        criteria.andUserIdEqualTo(userId);
        userRoleMapper.deleteByExample(ue);
        return 1;
    }

    //登录
    @Override
    public Map<String, Object> updateshowlogin(AdminUser a) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        AdminUserExample ae = new AdminUserExample();
        AdminUserExample.Criteria criteria = ae.createCriteria();
        criteria.andUserNameEqualTo(a.getUserName());
        List<AdminUser> list = adminUserMapper.selectByExample(ae);
        Map<String, Object> map = new HashMap<>();
        if (!(list.size() > 0)) {
            map.put("code", false);
            map.put("message", "账号不存在");
            return map;
        }
        if (list.get(0).getDepId() >= 3) {
            map.put("code", false);
            map.put("message", "账号已冻结请联系领导");
            return map;
        }
        if (!(MD5Utils.validPassword(a.getUserPwd(), list.get(0).getUserPwd()))) {
            AdminUser adminUser = list.get(0);
            int i = adminUser.getDepId() + 1;
            adminUser.setDepId(i);
            adminUserMapper.updateByPrimaryKeySelective(adminUser);
            map.put("code", false);
            if ((3 - i) == 0) {
                map.put("message", "你已输入3次错误密码现在将你账号冻结");
            } else {
                map.put("message", "密码错误,您再错误输入密码" + (3 - i) + "次,就会被冻结");
            }
            return map;
        }
        AdminUser adminUser = list.get(0);
        System.out.println(adminUser);
        map.put("code", true);
        map.put("massage", "登录成功");
        map.put("AdminUser",adminUser);

        adminUser.setDepId(0);
        adminUserMapper.updateByPrimaryKeySelective(adminUser);
        return map;
    }
}

